<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>跨浏览器处理xml</title>
	</head>

	<body>

	</body>
	<script>
		//跨浏览器返回xml Dom 对象
		function getXMLDOM(xmlStr) {
			var xmlDom = null;

			if(typeof window.DOMParser != 'undefined') { //w3c
				xmlDom = (new DOMParser).parseFromString(xmlStr, 'text/xml');
				var errors = xmlDom.getElementsByTagName('parsererror'); //得到解析错误标签的信息
				if(errors > 0) {
					throw new Error('手动抛出错误信息：' + errors[0].textContent);
				}

			} else if(typeof window.ActiveXObject != 'undefined') {
				//ie的不同版本
				var version = [
					'MSXML2.DOMDocument6.0',
					'MSXML2.DOMDocument3.0',
					'MSXML2.DOMDocument'

				];
				//循环判断支持哪个
				for(var i = 0; i < version.length; i++) {
					try {
						var xmlDom = new ActiveXObject(version[i]);

					} catch(e) {
						//错误跳过不处理
					}
				}

				xmlDom.loadXML(xmlStr);
				if(xmlDom.parseError != 0) {
					throw new Error('我的错误信息' + xmlDom.parseError.reason);

				}
				return xmlDom;
			} else {
				throw new Error('xmlDOM 对象');
			}

			return xmlDom;
		}

		//序列化xml兼容
		function serializerXml(xmlDom) {
			var xml = '';

			if(typeof window.XMLSerializer != 'undefined') {
				xml = (new XMLSerializer()).serializeToString(xmlDom);
			} else if(typeof xmlDom.xml != 'undefined') {
				xml = xmlDom.xml;
			}

			return xml;
		}

		//使用
		var xmlStr = '<root><user>lee</user></root>';
		var xmlDom = getXMLDOM(xmlStr);

		alert(serializerXml(xmlDom));
	</script>

</html>